﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Linguist.Language.NGram.Large
{
    /// <summary>
    /// Language model that reads whole model into memory. Useful
    /// for loading language models from resources or external locations.
    /// </summary>
    public class BinaryStreamLoader: BinaryLoader
    {
        byte[] modelData;
    
        public BinaryStreamLoader(String location, String format, Boolean
                applyLanguageWeightAndWip,
                float languageWeight, double wip,
                float unigramWeight)
            :base(format, applyLanguageWeightAndWip, languageWeight, wip,
                    unigramWeight)
           
        {        
            FileStream stream = new FileStream(location,FileMode.Open);
            loadModelLayout(stream);
        
            loadModelData(stream);
        }

    
        /**
        /// Reads whole data into memory
        /// 
        /// @param stream  the stream to load model from
        /// @throws IOException 
         */
        private void loadModelData(FileStream stream) 
        {
            modelData = new Byte[stream.Length];
            stream.Read(modelData,0,(int)stream.Length);
        }

        public byte[] loadBuffer(long position, int size)
        {
            byte[] result = new byte[size];
            Array.Copy(modelData, (int)position, result, 0, size);
            return result;
        }

    }
}
